package com.sx.art2.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.sx.art2.beans.Adress;
import com.sx.art2.beans.AuctionInfo;
import com.sx.art2.beans.User;
import com.sx.art2.service.AdressService;


@Controller
@SessionAttributes(types={User.class,Adress.class,List.class})
public class AdressController {
	
	@Autowired
	AdressService adressService;
	@RequestMapping("/adressList")
	public ModelAndView listAdress(HttpSession session) {
		
		ModelAndView mv=new ModelAndView();
		User user=(User)session.getAttribute("User");
		System.out.println(user.getUserId());
		List<Adress> list =adressService.list(user.getUserId());		
		mv.addObject("adressList", list);
		mv.setViewName("adress");
		return mv;		
	}
	@RequestMapping("/adressList1")
	public ModelAndView listAdress1(HttpSession session) {
		
		ModelAndView mv=new ModelAndView();
		User user=(User)session.getAttribute("User");
		System.out.println(user.getUserId());
		List<Adress> list =adressService.list(user.getUserId());		
		mv.addObject("adressList", list);
		mv.setViewName("jiesuan");
		return mv;		
	}
	@RequestMapping("/adressAdd")
	public ModelAndView addAdress(HttpSession session,Adress adress) {
		
		ModelAndView mv=new ModelAndView();		
		User user=(User)session.getAttribute("User");
		System.out.println("adress");
		System.out.println(user.getUserId());
		System.out.println(adress.getAdressProvince());
		System.out.println(adress.getAdressCity());
		adress.setUserId(user.getUserId());
		adressService.add(adress);
		mv.setViewName("redirect:/adressList");
		return mv;
	}
	@RequestMapping("/Order/adressAdd1")
	public ModelAndView addAdress1(HttpSession session,Adress adress) {
		
		ModelAndView mv=new ModelAndView();		
		User user=(User)session.getAttribute("User");
		adress.setUserId(user.getUserId());
		adressService.add(adress);
		AuctionInfo auc=(AuctionInfo) session.getAttribute("auctionInfo");
		int auctionInfoId=auc.getAuctionInfoId();
		mv.setViewName("redirect:/Order/getAddress?auctionInfoId="+auctionInfoId);
		return mv;
	}
	@RequestMapping("/adressDelete")
	public ModelAndView deleteAdress(int adressId) {
		ModelAndView mv=new ModelAndView();
		adressService.delete(adressId);
		mv.setViewName("redirect:/adressList");
		return mv;
	}
	@RequestMapping("/Order/adressDelete1")
	public ModelAndView deleteAdress1(HttpSession session, int adressId) {
		ModelAndView mv=new ModelAndView();
		adressService.delete(adressId);
		AuctionInfo auc=(AuctionInfo) session.getAttribute("auctionInfo");
		int auctionInfoId=auc.getAuctionInfoId();
		mv.setViewName("redirect:/Order/getAddress?auctionInfoId="+auctionInfoId);
		return mv;
	}
	/*@RequestMapping("adressSelectById")
	public ModelAndView selectById(int adressId) {
		
		ModelAndView mv=new ModelAndView();
		System.out.println("adressController");
		System.out.println(adressId);
		Adress adress=adressService.selectById(adressId);
		mv.addObject("Adress", adress);
		mv.setViewName("redirect:/adressList");
		return mv;
	}*/
	@RequestMapping("adressSelectById")
	public String selectById(int adressId,HttpServletRequest req,HttpServletResponse resp) {
		
		
		System.out.println("adressController");
		System.out.println(adressId);
		Adress adress=adressService.selectById(adressId);
		req.getSession().setAttribute("Adress", adress);
		System.out.println(adress);
		return "adressUpdate";
	}
	@RequestMapping("adressUpdate")
	public ModelAndView updateAdress(HttpSession session,Adress adress) {
		
		ModelAndView mv=new ModelAndView();
		Adress ad=(Adress) session.getAttribute("Adress");
		adress.setAdressId(ad.getAdressId());
		adressService.update(adress);
		mv.setViewName("redirect:/adressList");
		return mv;
	}	

}
